package com.example.black.myapplication.dragswip;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.black.myapplication.R;

import java.util.Collections;
import java.util.List;

public class QQAdapter extends RecyclerView.Adapter<QQAdapter.MyViewHolder> implements MyItemTouchHelper.ItemMoveListener {
    private List<QQMessage> list;

    public QQAdapter(List<QQMessage> list) {
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        QQMessage qqMessage = list.get(position);
        holder.iv_logo.setImageResource(qqMessage.getLogo());
        holder.tv_name.setText(qqMessage.getName());
        holder.tv_Msg.setText(qqMessage.getLastMsg());
        holder.tv_time.setText(qqMessage.getTime());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public boolean onMove(int srcPosition, int targetPosition) {
        Collections.swap(
                list
                , srcPosition
                , targetPosition
        );
        notifyItemMoved(srcPosition, targetPosition);
        return true;
    }

    @Override
    public void onSwipedHorizontal(int position) {
       // list.remove(position);//滑动就移除掉list的数据
       // notifyItemRemoved(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_logo;
        private TextView tv_name;
        private TextView tv_Msg;
        private TextView tv_time;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_logo = itemView.findViewById(R.id.iv_logo);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_Msg = itemView.findViewById(R.id.tv_lastMsg);
            tv_time = itemView.findViewById(R.id.tv_time);
        }

    }
}
